package indi.a709924470.scpwiki;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.sql.Ref;
import java.util.List;

import indi.a709924470.scpwiki.RecycleViewThings.itemDecorate;
import indi.a709924470.scpwiki.RecycleViewThings.rcvwHolder;
import indi.a709924470.scpwiki.beans.Constants;
import indi.a709924470.scpwiki.beans.TheBean;
import indi.a709924470.scpwiki.beans.WikiItems;
import indi.a709924470.scpwiki.beans.WikiSubItems;

public class SubRVAct extends AppCompatActivity {

    private List<WikiItems> wi;
    private String url;
    private static rcvwHolder rh = null;
    private SwipeRefreshLayout srl;

    //public SubRVAct(){this.wi = null;}

    //public SubRVAct(String s){
        //this.url = s;
    //}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_rv);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent i = getIntent();
        //this.wi = (List<WikiItems>) i.getSerializableExtra("WikiItems");
        this.url = i.getStringExtra("Url");

        /*ParseWikiPage pwp = new ParseWikiPage(url);
        if (!pwp.isSuccess()){
            Toast.makeText(this, "加载失败！", Toast.LENGTH_SHORT).show();
            hlr.sendEmptyMessageDelayed(0,500);
            return;
        }
        wi = pwp.pageitems();
        this.setTitle(pwp.getTitle());*/

        Log.d("SubRV", "onCreate: new size of WI = "+wi.size());

        RecyclerView rv = (RecyclerView) findViewById(R.id.innerRV);
        rh = new rcvwHolder(wi,this,HolderType.SubPage);
        rv.setAdapter(rh);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new itemDecorate(this, LinearLayout.HORIZONTAL));
        rh.notifyDataSetChanged();

        srl = (SwipeRefreshLayout) findViewById(R.id.sref);
        srl.setDistanceToTriggerSync(100);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Refresh().execute(url);
            }
        });
        srl.setRefreshing(true);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab2);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "正在刷新...", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                srl.setRefreshing(true);
            }
        });
    }

    private Handler hlr = new Handler() {
        @Override
        public void handleMessage(Message what){
            finish();
        }
    };

    class Refresh extends AsyncTask<String,Void,Boolean>{

        @Override
        protected void onPreExecute(){
            srl.setRefreshing(true);
        }

        @Override
        protected Boolean doInBackground(String... strings) {
            try {
                ParseWikiPage pwp = new ParseWikiPage(strings[0] == "" ? null : strings[0],"");
                wi = pwp.pageitems();
                rh.notifyDataSetChanged();
                setTitle(pwp.getTitle());
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean result){
            srl.setRefreshing(false);
            if (!result){
                Toast.makeText(getApplicationContext(),"加载失败.......",Toast.LENGTH_LONG);
                hlr.sendEmptyMessageDelayed(0,1000);
            }
        }
    }
}
